package com.nash.demands;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.widget.TabHost;

public class DocTabActivity extends TabActivity {

  public DocHeadActivity docHeadActivity;
  public DocStockActivity docStockActivity;
  public DocItemsActivity docItemsActivity;
  public final static String TBL_TEMP_ITEM = "temp_item";
  public final static String TBL_TEMP_ITEM_ID_TU = "id_tu";
  public final static String TBL_TEMP_ITEM_AMOUNT = "amount";
  public final static String TBL_TEMP_ITEM_PRICE = "price";
  private static final String CREATE_TEMP_TABLE = "create temporary table if not exists " + TBL_TEMP_ITEM + " (" + BaseColumns._ID + " integer primary key autoincrement not null," + TBL_TEMP_ITEM_ID_TU
      + " integer unique, " + TBL_TEMP_ITEM_AMOUNT + " integer not null," + TBL_TEMP_ITEM_PRICE + " numeric not null)";
  private boolean insertMode;
  private long demandId;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.doc_tab);
    DemandsActivity.db.execSQL(CREATE_TEMP_TABLE);
    DemandsActivity.db.execSQL("delete from " + TBL_TEMP_ITEM);

    Bundle b = getIntent().getExtras();
    boolean sended = b.getInt("addit", 0)==0?false:true;
    demandId = b.getLong("demand_id", 0);

    insertMode = demandId == 0;
    if (!insertMode) {
      DemandsActivity.db.execSQL("insert into " + TBL_TEMP_ITEM + " (" + TBL_TEMP_ITEM_ID_TU + "," + TBL_TEMP_ITEM_AMOUNT + "," + TBL_TEMP_ITEM_PRICE + ") select " + Helper.TBL_DEMAND_LINE_ID_TU + ","
          + Helper.TBL_DEMAND_LINE_AMOUNT + "," + Helper.TBL_DEMAND_LINE_PRICE + " from " + Helper.TBL_DEMAND_LINE + " where " + Helper.TBL_DEMAND_LINE_ID_DEMAND + "=" + demandId);
    }
    // long agentId = b.getLong("agent_id", 0);
    // String agentName = b.getString("agent_name");

    // vw1.setText("Name: " + name);
    // vw2.setText("State: " + String.valueOf(value));

    // Tabs
    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, DocHeadActivity.class);
    intent.putExtras(b);
    spec = tabHost.newTabSpec("document").setIndicator("Документ", res.getDrawable(R.drawable.ic_tab_dochead)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, DocStockActivity.class);
    spec = tabHost.newTabSpec("docStocks").setIndicator("Остатки", res.getDrawable(R.drawable.ic_tab_docstock)).setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, DocItemsActivity.class);
    intent.putExtras(b);
    spec = tabHost.newTabSpec("docItems").setIndicator("Позиции", res.getDrawable(R.drawable.ic_tab_docitems)).setContent(intent);
    tabHost.addTab(spec);

    if (sended)
      tabHost.getTabWidget().getChildTabViewAt(1).setEnabled(false);
    tabHost.setCurrentTab(0);
  }

  public void myOnBackPressed() {
    super.onBackPressed();
    /*
    if (docHeadActivity.sended || !docHeadActivity.isEdited) {
      super.onBackPressed();
    } else {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("Заявка изменена и не сохранена. Нажмите клавишу возврата чтобы вернуться к заявке.").setPositiveButton("Сохранить и выйти", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          docHeadActivity.saveDemand();
          setResult(RESULT_OK);
          finish();
        }
      });
      builder.setNegativeButton("Закрыть без сохранения", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
          setResult(RESULT_CANCELED);
          finish();
        }
      });
      builder.show();
    }*/
  }
}